﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
using System.Web.Script.Serialization;
using System.Web.UI.WebControls;

public partial class ClientMaster : System.Web.UI.Page, ICallbackEventHandler
{
    Dictionary<string, string> ClientRequest = null;
    Dictionary<string, string> ClientResponse = null;
    JavaScriptSerializer serializer;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsCallback)
        {
            CallBackReference();
            if (!IsPostBack)
            {
                if (SessionInfo.prop_getObj.EmployeeID == 0)
                    Response.Redirect("login.aspx");
                else
                {
                    hdnExportToExcelFilePath.Value = GlobalDefaultValues.DHTMLXExportToExcelFilePath;
                    PLMstFormMasters objRights = CommonUtil.CheckRightsForForm("2");
                    if (objRights.ViewAccess == false)
                        Response.Redirect("Login.aspx");
                    txtSearchClientName.Focus();

                    grdResult.DataSource = new BL_Client().BL_fnGetClientList(new PLClient());
                    grdResult.DataBind();
                }
            }
        }
    }

    // Override the Render method to ensure that this control
    // is nested in an HtmlForm server control, between a <form runat=server>
    // opening tag and a </form> closing tag.
    protected override void Render(HtmlTextWriter writer)
    {
        // Ensure that the control is nested in a server form.
        if (Page != null)
        {
            Page.VerifyRenderingInServerForm(this);
        }
        base.Render(writer);
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        /* Verifies that the control is rendered */
    }



    #region callback functions
    public void CallBackReference()
    {
        string strReference = Page.ClientScript.GetCallbackEventReference(this, "args", "getResponse", "context", "ClientCallbackError", true);
        string strScript = " function UseCallback(args,context) { " + strReference + " ; } ";
        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "UseCallback", strScript, true);
    }

    public string GetCallbackResult()
    {
        System.Web.Script.Serialization.JavaScriptSerializer oSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
        oSerializer.MaxJsonLength = GlobalDefaultValues.MaxJsonLength;
        return oSerializer.Serialize(ClientResponse);
    }

    public void RaiseCallbackEvent(string strEventArgument)
    {
        try
        {
            serializer = new JavaScriptSerializer();
            ClientRequest = serializer.Deserialize<Dictionary<string, string>>(strEventArgument);
            ClientResponse = new Dictionary<string, string>();
            string Track = ClientRequest["Track"];
            ClientResponse = new Dictionary<string, string>();
            ClientResponse["Track"] = Track;
            if (SessionInfo.prop_getObj.EmployeeID == 0)
            {
                ClientResponse["SessionTimeOut"] = "1";
            }
            else
            {
                ClientResponse["SessionTimeOut"] = "0";
                if (Track.Equals("GetSearchResult"))
                {
                    UI_SearchResult();
                }
                else if (Track.Equals("InsertEditDetails"))
                {
                    PLClient objClient = new PLClient();
                    objClient.ClientID = TrynParse.parseInt(ClientRequest["ClientID"]);
                    objClient.ClientName = ClientRequest[txtClientName.ID]; 
                    objClient.Industry = ClientRequest[txtIndustry.ID];
                    objClient.Address1 = ClientRequest[txtAddress1.ID];
                    objClient.Address2 = ClientRequest[txtAddress2.ID];
                    objClient.City = ClientRequest[txtCity.ID]; 
                    objClient.PinCode = ClientRequest[txtPinCode.ID];
                    objClient.Phone = ClientRequest[txtPhone.ID]; 
                    objClient.Fax = ClientRequest[txtFax.ID];
                    objClient.Email = ClientRequest[txtEmail.ID];
                    objClient.PAN = ClientRequest[txtPAN.ID];
                    objClient.ContactPersonName = ClientRequest[txtContactPersonName.ID]; 
                    objClient.ContactPersonPhone = ClientRequest[txtContactPersonPhone.ID];
                    objClient.ContactPersonEmail = ClientRequest[txtContactPersonEmail.ID]; 
                    objClient.TIN = ClientRequest[txtTIN.ID];
                    objClient.IsServiceTaxAllowed = TrynParse.parseBool(ClientRequest[chkIsServiceTaxAllowed.ID]);
                    objClient.IsActive = TrynParse.parseBool(ClientRequest[chkIsActive.ID]);

                    try
                    {
                        int Result = new BL_Client().BL_InsertUpdateClient(objClient);
                        if (Result > 0)
                        {
                            ClientResponse["strSaveMessage"] = GlobalDefaultValues.SaveMessage.Replace("XXX", "Client");
                            ClientResponse["strUpdateMessage"] = GlobalDefaultValues.UpdateMessage.Replace("XXX", "Client");
                            ClientResponse["IsSuccess"] = "1";
                            UI_SearchResult();
                        }
                        else
                        {
                            ClientResponse["strMessage"] = GlobalDefaultValues.SaveErrorMessage.Replace("XXX", "Client");
                            ClientResponse["IsSuccess"] = "0";
                        }
                    }
                    catch (Exception ex)
                    {
                        ClientResponse["strMessage"] = ex.Message;
                        CommonUtil.Elmah_SignalException(ex, this.Context, "Client - Method: RaiseCallbackEvent(), Track-InsertEditDetails");
                        ClientResponse["IsSuccess"] = "0";
                    }

                }
            }
        }
        catch (Exception ex)
        {
            CommonUtil.Elmah_SignalException(ex, this.Context, "User Master - Method: RaiseCallbackEvent()");
        }
    }


    private void UI_SearchResult()
    {
        PLClient objClient = new PLClient();
        objClient.ClientName = ClientRequest[txtSearchClientName.ID];
        if (ClientRequest[cboStatus.ID] == "2")
            objClient.SearchIsActive = null;
        else
        {
            if (ClientRequest[cboStatus.ID] == "0")
                objClient.SearchIsActive = false;
            else
                objClient.SearchIsActive = true;
        }
        SessionInfo.prop_getObj.ClientMasterSearchCriteria = objClient;
        grdResult.DataSource = new BL_Client().BL_fnGetClientList(objClient);
        grdResult.DataBind();
        ClientResponse["grdResult"] = CommonUtil.GetRenderControl(grdResult);
    }

    protected void grdResult_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[0].Style["display"] = "none"; 
            e.Row.Cells[1].Text = (e.Row.RowIndex + 1).ToString();
        }
        else if (e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Cells[0].Style["display"] = "none"; 
        }
    }
    #endregion

}